/*
 *
 */
package org.jiscinvolve.astrodabis.barberry.common.inport.startable;

import java.util.Map;
import java.util.HashMap;
import java.util.Iterator;
import java.io.IOException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import uk.ac.starlink.table.StarTable;

import org.jiscinvolve.astrodabis.barberry.common.core.Job;
import org.jiscinvolve.astrodabis.barberry.common.core.Job.AnnotType;

import org.jiscinvolve.astrodabis.barberry.common.core.Tag;
import org.jiscinvolve.astrodabis.barberry.common.core.Account;
import org.jiscinvolve.astrodabis.barberry.common.core.Womble;
import org.jiscinvolve.astrodabis.barberry.common.core.Tagged;

import org.jiscinvolve.astrodabis.barberry.common.data.TapRow;
import org.jiscinvolve.astrodabis.barberry.common.data.TapJoin;
import org.jiscinvolve.astrodabis.barberry.common.data.TapTable;
import org.jiscinvolve.astrodabis.barberry.common.data.TapColumn;
import org.jiscinvolve.astrodabis.barberry.common.data.TapService;

import org.jiscinvolve.astrodabis.barberry.common.data.RowTag;
import org.jiscinvolve.astrodabis.barberry.common.data.JoinTag;

/**
 *
 */
public class TaggedStarTableParser
extends AbstractStarTableParser
implements Tagged.Parser
    {
    /**
     * Our debug logger.
     * 
     */
    private static Log log = LogFactory.getLog(TaggedStarTableParser.class);

    private Job job ;
    public Job job()
        {
        return this.job ;
        }


    public TaggedStarTableParser(Job job, Iterator<StarTable> tables)
    throws IOException
        {
        super(
            tables
            );
        this.job = job ;
        }

    /**
     * Process the next row of data.
     *
     */
    @Override
    public Tagged next()
        {
        nextRow();

        //log.debug("Checking tag name [" + job.tagNameSrc() + "]");
        String tagname = null ;
        switch (job.tagNameSrc())
            {
            case INPAGE :
                tagname = job.tagName();
                break ;

            case INDATA :
            default :
                tagname = getString(
                    "tagname"
                    );
                break ;

            }

        //log.debug("Checking tag data [" + job.tagDataSrc() + "]");
        Double tagdata = null ; 
        switch (job.tagDataSrc())
            {
            case INPAGE :
                tagdata = job.tagData();
                break ;

            case INDATA :
                tagdata = getDouble(
                    "tagvalue"
                    );
                break ;

            case NONE :
            default :
                tagdata = null;
                break ;

            }

        //log.debug("Found tag name [" + job.tagNameSrc() + "][" + tagname + "]");
        //log.debug("Found tag data [" + job.tagDataSrc() + "][" + tagdata + "]");
        //log.debug("Creating tag");

        Tag tag = job.owner().tags().createQuick(
            tagname
            );

        //log.debug("Checking row one [" + job.colNameOne() + "]");
        TapRow rowone = job.columnOne().rows().createQuick(
            getString(
                job.colNameOne()
                )
            );

        if (job.annotType() == AnnotType.SINGLE)
            {

            //log.debug("Creating row tag");
            return rowone.tags().createQuick(
                job,
                tag,
                tagdata
                );

            }

        else {

            //log.debug("Checking row two [" + job.colNameTwo() + "]");
            TapRow rowtwo = job.columnTwo().rows().createQuick(
                getString(
                    job.colNameTwo()
                    )
                );

            //log.debug("Checking join");
            TapJoin join = rowone.joins().createQuick(
                rowtwo
                );

            //log.debug("Creating join tag");
            return join.tags().createQuick(
                job,
                tag,
                tagdata
                );
            }
        }
    }

